package com.sright.bcard.mini.web.task;

import com.sright.bcard.common.service.share.utils.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class DateTimeJob extends QuartzJobBean {

    @Autowired
    private JedisUtil jedisUtil;

    private static final String DATETIME_KEY = "DATETIME_KEY1";

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        Boolean lock = jedisUtil.getLock(DATETIME_KEY, 300000L);
        if(lock) {
            try {
                //获取JobDetail中关联的数据
                String msg = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("msg");
                log.info("current time :"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "---" + msg);
            } catch (Throwable throwable) {
                log.error("error : {}", throwable);
            } finally {
                jedisUtil.releaseLock(DATETIME_KEY);
            }
        } else {
            log.info("dint't get lock");
        }

    }
}
